package com.wn.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.dto.AddCourseDTO;
import com.wn.pojo.Course;
import com.wn.service.ICourseService;
import com.wn.utils.PageData;
import com.wn.utils.Result;
import com.wn.vo.CoursePublishVo;
import com.wn.vo.CourseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-06-20
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {

    @Autowired
    private ICourseService courseService;

    @PostMapping
    public Result addCourse(@RequestBody AddCourseDTO addCourseDTO) {
        // 添加课程
        String courseId = courseService.saveCourse(addCourseDTO);
        return toDataResult(courseId);
    }

    @GetMapping("/{courseId}")
    public Result findAddCourseDTO(@PathVariable("courseId") String courseId) {
        AddCourseDTO addCourseDTO = courseService.findAddCourseDTO(courseId);
        return toDataResult(addCourseDTO);
    }

    //发布课程的查询
    @ApiOperation("课程发布的展示的查询操作")
    @GetMapping("/publish/find/{courseId}")
    public Result findPublish(@PathVariable("courseId") String courseId) {
        CoursePublishVo vo = courseService.findPublish(courseId);
        return toDataResult(vo);
    }

    @ApiOperation("课程发布的展示的查询操作")
    @PutMapping("/publish/{courseId}")
    public Result coursePublish(@PathVariable("courseId") String courseId) {

        boolean flag = courseService.publish(courseId);
        return toResult(flag);

    }

    @ApiOperation("查询课程")
    @GetMapping("/search/{currentPage}/{pageSize}")
    public Result findCourse(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("pageSize") Integer pageSize) {

        PageData page = courseService.findPageCourse(currentPage, pageSize);

        return toDataResult(page);
    }

    // 最近的 课程发布的前4条记录
    @ApiOperation("查询热门课程")
    @GetMapping("/hot")
    public Result findHotCourse() {

        List<CourseVo> list = courseService.findHotPageCourse();

        return toDataResult(list);
    }

    @ApiOperation("分页查询vo查询课程")
    @GetMapping("/all/{currentPage}/{pageSize}")
    public Result findAllCourse(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("pageSize") Integer pageSize) {

        PageData page = courseService.findAllPageCourse(currentPage, pageSize);

        return toDataResult(page);
    }

    @ApiOperation("通过id查询课程详情")
    @GetMapping("/detail/{courseId}")
    public Result findCourseDetail(@PathVariable("courseId") String courseId) {
        AddCourseDTO addCourseDTO = courseService.findAddCourseDTO(courseId);
        CourseVo coursevo = new CourseVo();
        BeanUtils.copyProperties(addCourseDTO, coursevo);
        return toDataResult(coursevo);
    }
}
